// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({ env: 'online-00000' })

const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let openid = wxContext.OPENID;

  if(event.openid){
    openid=event.openid;
  }

  const act = event.act;

  switch (act) {

    case 'join':
      let data = event.data;
      let proCount = await db.collection('joiner').where({
        openid
      }).count()
      if (proCount.total == 1) {
        return {
          mes: '已经报名,无需重复报名'
        }
      }
      else {
        data.openid = openid;
        if(event.btype){
          data.btype = event.btype
        }
        await db.collection('joiner').add({
          data: data
        });
        let curDate = new Date(data.time + 3600 * 1000 * 8)
        let time = `${curDate.getFullYear()}-${curDate.getMonth() +
          1}-${curDate.getDate()} ${curDate.getHours()}:${curDate.getMinutes()}:${curDate.getSeconds()}`;
        let time1 = `${curDate.getFullYear()}-${curDate.getMonth() +
          1}-${curDate.getDate()} 19:00:00`;
        //发送模板消息
        if(event.openid){
          // const sendResult= await cloud.callFunction({
          //   name:'sendqqmb',
          //   data:{
          //     params:{
          //       touser: event.openid,
          //       formId: event.formId,
          //       templateId: '6f07db0725727cda13085a456ddfda41',
          //       page: 'pages/result/result',
          //       data: {
          //         keyword1: {
          //           value: '炉石选牌活动',
          //         },
          //         keyword2: {
          //           value: time1,
          //         },
          //         keyword3: {
          //           value: data.userInfo.nickName,
          //         },
          //         keyword4: {
          //           value: data.userInfo.gender == 1 ? '男' : '女',
          //         },
          //         keyword5: {
          //           value: time
          //         }
          //       }
          //     }
          //   }
          // })
        }
        else{
          const sendResult = await cloud.openapi.templateMessage.send({
            touser: openid,
            templateId: 'qsh7n2lIJaRvIwF_kjfd5CaRRSW6o5ySLuf3N9uM8PI',
            formId: event.formId,
            page: 'pages/result/result',
            data: {
              keyword1: {
                value: '炉石选牌活动',
              },
              keyword2: {
                value: time1,
              },
              keyword3: {
                value: data.userInfo.nickName,
              },
              keyword4: {
                value: data.userInfo.gender == 1 ? '男' : '女',
              },
              keyword5: {
                value: time
              },
              keyword6: {
                value: '报名成功'
              }
            }
          });
        }

        return {
          mes: '报名成功'
        }
      }
      break;

    case 'check':
      let proCount2 = await db.collection('joiner').where({
        openid
      }).count();

      return {
        status:proCount2.total==1?true:false
      }
  }
}